package com.ruoyi.web.controller.quality;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.quality.domain.bo.QualityAnalysisBo;
import com.ruoyi.quality.domain.bo.QualityPhysicalReportBo;
import com.ruoyi.quality.domain.vo.QualityAnalysisVo;
import com.ruoyi.quality.service.IQualityAnalysisService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实物质量分析
 *
 * @author ruoyi
 * @date 2023-08-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/quality/analysis")
public class QualityAnalysisController extends BaseController {

    private final IQualityAnalysisService iQualityAnalysisService;

    /**
     * 查询实物质量分析列表
     */
    @SaCheckPermission("quality:analysis:list")
    @GetMapping("/list")
    public TableDataInfo<QualityAnalysisVo> list(QualityAnalysisBo bo, PageQuery pageQuery) {
        return iQualityAnalysisService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出实物质量分析列表
     */
    @SaCheckPermission("quality:analysis:export")
    @Log(title = "实物质量分析", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(QualityAnalysisBo bo, HttpServletResponse response) {
        Map<String, String> map = new HashMap<>();
        List<QualityAnalysisVo> list = iQualityAnalysisService.queryList(bo);
        list.forEach(item-> item.setDeptFullName(item.getDeptAbbreviation()));
        ExcelUtil.exportTemplate(CollUtil.newArrayList(map, list), "单列表.xlsx", "excel/自主管理情况汇总表.xlsx", response);
    }

    /**
     * 获取实物质量分析详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("quality:analysis:query")
    @GetMapping("/{id}")
    public R<QualityAnalysisVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iQualityAnalysisService.queryById(id));
    }

    /**
     * 新增实物质量分析
     */
    @SaCheckPermission("quality:analysis:add")
    @Log(title = "实物质量分析", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody QualityAnalysisBo bo) {
        return toAjax(iQualityAnalysisService.insertByBo(bo));
    }

    /**
     * 修改实物质量分析
     */
    @SaCheckPermission("quality:analysis:edit")
    @Log(title = "实物质量分析", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody QualityAnalysisBo bo) {
        return toAjax(iQualityAnalysisService.updateByBo(bo));
    }

    /**
     * 删除实物质量分析
     *
     * @param ids 主键串
     */
    @SaCheckPermission("quality:analysis:remove")
    @Log(title = "实物质量分析", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iQualityAnalysisService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 查询当月通报列表
     */
    @SaCheckPermission("quality:analysis:list")
    @GetMapping("/listReport")
    public R<List<Object>> listReport(QualityPhysicalReportBo bo)
    {
        List<Object> list = iQualityAnalysisService.getAnalysisReportList(bo);
        return R.ok(list);
    }

    /**
     * 查询当月通报列表
     */
    @SaCheckPermission("quality:analysis:list")
    @GetMapping("/getTopList")
    public R<List<Map<String, Object>>> getTopList()
    {
        List<Map<String, Object>> list = iQualityAnalysisService.getAllTop(5);
        return R.ok(list);
    }

    /**
     * 查询当月通报列表
     */
    @SaCheckPermission("quality:analysis:list")
    @GetMapping("/getTopPreview/{ids}")
    public R<List<Map<String, Object>>> getTopPreview(@NotEmpty(message = "主键不能为空")
                                                          @PathVariable Long[] ids)
    {
        List<Map<String, Object>> list = iQualityAnalysisService.getTopPreview(ids);
        return R.ok(list);
    }
}
